<template>
  <div class="regist-container">
    <el-form ref="registForm" :model="registForm" status-icon :regists="registRules" label-width="100px" class="regist-form">
      <div class="title-container">
        <h3 class="title">汽车美容店管理系统</h3>
      </div>
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="registForm.username"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="off"
        />
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input
          v-model="registForm.pass"
          type="password"
          name="password"
          tabindex="2"
          autocomplete="off"
        />
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          v-model="registForm.checkPass"
          type="password"
          name="checkPassword"
          tabindex="2"
          autocomplete="off"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('registForm')">提交</el-button>
        <el-button @click="resetForm('registForm')">重置</el-button>
        <el-link @click="backLogin">已有帐号？去登录</el-link>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { regist } from '@/api/user'
import { Message } from 'element-ui'

export default {
  name: 'Regist',
  data() {
    const validatePass = (regist, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.registForm.checkPass !== '') {
          this.$refs.registForm.validateField('checkPass')
        }
        callback()
      }
    }
    const validatePass2 = (regist, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.registForm.pass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      registForm: {
        username: '',
        pass: '',
        checkPass: ''
      },
      registRules: {
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const { username, pass } = this.registForm
          new Promise((resolve, reject) => {
            regist({ username: username.trim(), password: pass }).then(response => {
              const res = response.data
              if (res.status !== 200) {
                Message({
                  message: res.message,
                  type: 'error',
                  duration: 5 * 1000
                })
              } else {
                Message({
                  message: res.message,
                  type: 'success',
                  duration: 5 * 1000
                })
                this.resetForm('registForm')
              }
            })
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    backLogin() {
      this.$router.push({ path: '/login' })
    }
  }
}
</script>

<style lang="scss">

</style>

